
//
//  BorderSearcuViewController.m
//  DistributorAB
//
//  Created by 主事丫环 on 2019/6/25.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BorderSearcuViewController.h"
#import "BorderAminListmodel.h"
#import "UITableViewFootview.h"
#import "BshopAminTableViewCell.h"
#import "BorderContentViewController.h"

@interface BorderSearcuViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *myTableView;
@property(nonatomic,strong)NSMutableArray *mydateSource;
@property(nonatomic,assign)NSInteger dayind;
@property(nonatomic,strong)UITableViewFootview *footview;
@property(nonatomic,strong)UITextField *searchTf;
@end

@implementation BorderSearcuViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    UITextField *search = [[UITextField alloc]init];
    [search createLeftViewImage:@"orderSarrch"];
        search.leftView.frame = CGRectMake(20, 0, 25, 25);
    search.delegate = self;
    self.navigationItem.titleView = search;
    search.returnKeyType = UIReturnKeySearch;
    search.backgroundColor = [UIColor colorWithHexString:@"#e5e5e5"];
    self.searchTf = search;
    [self.rightbutton setTitle:@"搜索" forState:(UIControlStateNormal)];
    self.rightbutton.titleLabel.font= FontSize(13);
    
    UIButton *imgv = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [imgv setImage:imgname(@"tfchja") forState:(UIControlStateNormal)];
    search.rightView = imgv;
    search.rightViewMode = UITextFieldViewModeAlways;
    [imgv addTarget:self
             action:@selector(searchAction) forControlEvents:(UIControlEventTouchUpInside)];
    self.mydateSource  = [NSMutableArray arrayWithCapacity:1];
    // Do any additional setup after loading the view.
}

-(void)searchAction{
    self.searchTf.text = @"";
}

- (void)rightClick{
    [self.searchTf resignFirstResponder];
    [self request];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.searchTf resignFirstResponder];
    [self request];
    return YES;
}

-(void)request{
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"name"] = _searchTf.text;
    [NetWorkTool POST:BshopshopOrderList param:param success:^(id dic) {
        [self.mydateSource removeAllObjects];
        
        [self.mydateSource addObjectsFromArray:[BorderAminListmodel mj_objectArrayWithKeyValuesArray:[dic objectForKey:@"data"]]];
        
        [self.myTableView reloadData];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.myTableView.mj_header endRefreshing];
        
        //NSString *label = [NSString stringWithFormat:@"%lu",(unsigned long)self.mydateSource.count];
        //NSDictionary *dict =[[NSDictionary alloc] initWithObjectsAndKeys:label,@"two", nil];
        //NSNotification *notification =[NSNotification notificationWithName:@"tongzhiRefish" object:nil userInfo:dict];
        ////通过通知中心发送通知
        //[[NSNotificationCenter defaultCenter] postNotification:notification];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
        [self.mydateSource removeAllObjects];
        [self.myTableView reloadData];
        [self.myTableView.mj_header endRefreshing];
        
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.myTableView.mj_header endRefreshing];
        
        [self.mydateSource removeAllObjects];
        [self.myTableView reloadData];
    } needUser:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    BorderAminListmodel *model = _mydateSource[section];
    return model.pros.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _mydateSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *heiader = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 0.1)];
    return heiader;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    BorderAminListmodel *model = _mydateSource[section];
    
    NSInteger ind = [model.state integerValue];
    if (ind == 1) {
        return 154;
    } else {
        return 114;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    BorderAminListmodel *model = _mydateSource[section];
    _footview = [[UITableViewFootview alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, 154)];
    
    NSInteger ind = [model.state integerValue];
    if (ind == 1) {
        self.footview.height = 154;
        kWeakSelf;
        _footview.myblco = ^(NSString * _Nonnull str) {
            //发货
            [weakSelf fahuoaction:model];
        };
    } else {
        self.footview.height = 114;
        [_footview.fahupaction removeFromSuperview];
    }
    
    if ([model.send_type_id integerValue] == 0) {
        _footview.wuliutypeImage.image = imgname(@"shopziti");
    }  else if ([model.send_type_id integerValue] == 1){
        _footview.wuliutypeImage.image = imgname(@"shopwuliu");
    } else if ([model.send_type_id integerValue] == 2){
        _footview.wuliutypeImage.image = imgname(@"shopdidipeisong");
    }
    _footview.gongjiLB.text = [NSString stringWithFormat:@"共计%.0f件产品",model.count];
    _footview.priceLB.text = [NSString stringWithFormat:@"¥%@",model.count_money];
    _footview.addnameLb.text = [NSString stringWithFormat:@"%@",model.user_name];
    _footview.addphoneLB.text = [NSString stringWithFormat:@"%@",model.phone];
    _footview.addcontentLB.text = [NSString stringWithFormat:@"%@",model.address];
    return _footview;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 140;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BshopAminTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BshopAminTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    BorderAminListmodel *model = [_mydateSource safeObjectAtIndex:indexPath.section];
    
    ProsModel *poM0del = [model.pros safeObjectAtIndex:indexPath.row];
    poM0del.state = model.state;
    poM0del.create_time1 = model.create_time;
    poM0del.order_number = model.order_number;
    
    [cell refashwithmodel:poM0del];
    kWeakSelf;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BorderAminListmodel *model = _mydateSource[indexPath.section];
    BorderContentViewController *dfesd = [[BorderContentViewController alloc]init];
    dfesd.model = model;
    dfesd.mhyblcok = ^(NSInteger ind) {
        [self request];
    };
    [self.navigationController pushViewController:dfesd animated:YES];
}

-(void)fahuoaction:(BorderAminListmodel *)model{
    
    UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"确定现在发货？" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    
    [alertCon addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        KMyLog(@"取消");
    }]];
    [alertCon addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        KMyLog(@"确定");
        NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
        param[@"id"] = NOTNIL(model.userID);
        
        [NetWorkTool POST:BshopdoSendProt param:param success:^(id dic) {
            ShowToastWithText(@"发货成功");
            [self request];
            
        } other:^(id dic) {
            ShowToastWithText(dic[@"msg"]);
        } fail:^(NSError *error) {
            
        } needUser:YES];
    }]];
    
    [self presentViewController:alertCon animated:YES completion:nil];
}

-(UITableView *)myTableView{
    if (!_myTableView) {
        _myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kNaviHeight, KWIDTH, KHEIGHT-kNaviHeight) style:(UITableViewStyleGrouped)];
        _myTableView.backgroundColor = [UIColor clearColor];
        _myTableView.delegate = self;
        _myTableView.dataSource = self;
        _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _myTableView.estimatedRowHeight = 90;
        _myTableView.rowHeight = UITableViewAutomaticDimension;
        _myTableView.tableFooterView = [UIView new];
        //_myTableView.placeHolderView = [[KKTableViewNoDataView alloc]initWithFrame:_myTableView.bounds image:imgname(@"tableviewPlaseholder") viewClick:^{
        //    [_myTableView.mj_header beginRefreshing];
        //}];
        _myTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self request];
        }];
        [_myTableView registerNib:[UINib nibWithNibName:@"BshopAminTableViewCell" bundle:nil] forCellReuseIdentifier:@"BshopAminTableViewCell"];
        adjustInset(_myTableView);
        [self.view addSubview:_myTableView];
    }
    return _myTableView;
}

@end


